package com.foreknow.view;

import com.foreknow.bean.Users;
import com.foreknow.service.UsersService;
import com.foreknow.service.impl.UserServiceImpl;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Login extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JPasswordField passwordField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Login frame = new Login();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Login() {
        setTitle("\u767B\u5F55");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JButton btnNewButton = new JButton("\u767B\u5F55");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                //登录
//				System.out.println("login");
                //调用
                UsersService service = new UserServiceImpl();
                String name = textField.getText();
                String password = passwordField.getText();
                Users user = service.query(name,password);
//                System.out.println(user.getAdminname());
                //新窗口

                MainWindow m = new MainWindow();
                Login.this.dispose();//隐藏当前窗口

            }
        });
        btnNewButton.setToolTipText("\u767B\u5F55");
        btnNewButton.setBounds(45, 209, 93, 23);
        contentPane.add(btnNewButton);

        JLabel label = new JLabel("\u7528\u6237\u540D");
        label.setBounds(59, 46, 54, 15);
        contentPane.add(label);

        JLabel label_1 = new JLabel("\u5BC6\u7801");
        label_1.setBounds(59, 102, 54, 15);
        contentPane.add(label_1);

        JButton button = new JButton("\u53D6\u6D88");
        button.addActionListener(new ActionListener() {
            //取消
            public void actionPerformed(ActionEvent e) {
                System.out.println("cancel");


            }
        });
        button.setBounds(295, 209, 93, 23);
        contentPane.add(button);

        JButton button_1 = new JButton("\u6CE8\u518C");
        button_1.addActionListener(new ActionListener() {
            //注册
            public void actionPerformed(ActionEvent e) {
                System.out.println("register");
                UserServiceImpl userService = new UserServiceImpl();
                String username = textField.getText();
                String password = passwordField.getText();
                Users user = new Users();
                user.setAdminname(username);
                user.setPassword(password);
                int a = userService.add(user);
                if(a==1){
                    System.out.println("succuss...");
                }else {
                    System.out.println("fail...");
                }
            }
        });
        button_1.setBounds(173, 209, 93, 23);
        contentPane.add(button_1);

        textField = new JTextField();
        textField.setBounds(123, 43, 174, 18);
        contentPane.add(textField);
        textField.setColumns(10);

        passwordField = new JPasswordField();
        passwordField.setBounds(123, 99, 174, 18);
        contentPane.add(passwordField);
    }
}